package com.fan.servlet.provider;

import com.alibaba.fastjson.JSONArray;
import com.fan.pojo.Provider;
import com.fan.pojo.User;
import com.fan.service.provider.ProviderService;
import com.fan.service.provider.ProviderServiceImpl;
import com.fan.util.Constants;
import com.fan.util.PageSupport;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ProviderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("query")){
            this.query(req,resp);
        }else if (method.equals("add")){
            this.add(req,resp);
        }else if (method.equals("delprovider")){
            this.delprovider(req,resp);
        }else if (method.equals("view")){
            this.getProviderById(req,resp);
        }else if (method.equals("modify")){
            this.modify(req,resp);
        }else if (method.equals("modifyexe")){
            this.modifyexe(req,resp);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }



    public void query(HttpServletRequest req, HttpServletResponse resp){

        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");
        String pageIndex = req.getParameter("pageIndex");
        List<Provider> proList = null;
        User user = null;
        ProviderService providerService = new ProviderServiceImpl();
        int pageSize = 5;
        int currentPageNo = 1;


        if (queryProCode == null){
            queryProCode = "";
        }
        if (queryProName == null){
            queryProName = "";
        }
        if (pageIndex != null){
            currentPageNo = Integer.parseInt(pageIndex);
        }

        int totalCount = providerService.getProviderCount(queryProCode, queryProName);
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);
        int totalPageCount = pageSupport.getTotalPageCount();

        if (currentPageNo<1){
            currentPageNo = 1;
        }else if (currentPageNo>totalPageCount){
            currentPageNo = totalPageCount;
        }

        proList = providerService.getProviderList(queryProCode, queryProName, currentPageNo, pageSize);
        req.setAttribute("providerList",proList);
        req.setAttribute("totalCount",totalCount);
        req.setAttribute("currentPageNo",currentPageNo);
        req.setAttribute("totalPageCount",totalPageCount);
        req.setAttribute("queryProCode",queryProCode);
        req.setAttribute("queryProName",queryProName);

        user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        req.setAttribute("user",user);

        try {
            req.getRequestDispatcher("providerlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proDesc = req.getParameter("proDesc");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");

        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProDesc(proDesc);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setCreationDate(new Date());
        provider.setCreatedBy(((User)req.getSession().getAttribute(Constants.USER_SESSION)).getId());

        ProviderService providerService = new ProviderServiceImpl();
        if (providerService.addProvider(provider)){
            resp.sendRedirect(req.getContextPath()+"/jsp/provider.do?method=query");
        }else {
            req.getRequestDispatcher("provideradd.jsp").forward(req,resp);
        }
    }

    public void delprovider(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("proid");
        Integer delId = 0;
        try {
            delId = Integer.parseInt(id);
        }catch (Exception e){
            delId = 0;
        }
        HashMap<String,String> resultMap = new HashMap<>();
        if (delId <= 0){
            resultMap.put("delResult","notexist");
        }else {
            ProviderService providerService = new ProviderServiceImpl();
            if (providerService.deleteProviderById(delId)){
                resultMap.put("delResult","true");
            }else {
                resultMap.put("delResult","false");
            }
        }
        resp.setContentType("application/json");
        PrintWriter outPrintWriter = resp.getWriter();
        outPrintWriter.write(JSONArray.toJSONString(resultMap));
        outPrintWriter.flush();
        outPrintWriter.close();
    }

    public void getProviderById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("proid");
        if (!StringUtils.isNullOrEmpty(id)){
            ProviderService providerService = new ProviderServiceImpl();
            Provider provider = providerService.getProviderById(id);
            req.setAttribute("provider",provider);
            req.getRequestDispatcher("/jsp/providerview.jsp").forward(req,resp);
        }
    }

    public void modify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("proid");
        if (!StringUtils.isNullOrEmpty(id)){
            ProviderService providerService = new ProviderServiceImpl();
            Provider provider = providerService.getProviderById(id);
            req.setAttribute("provider",provider);
            req.getRequestDispatcher("/jsp/providermodify.jsp").forward(req,resp);
        }
    }

    public void modifyexe(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("proid");
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proDesc = req.getParameter("proDesc");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        if (!StringUtils.isNullOrEmpty(id)){
            ProviderService providerService = new ProviderServiceImpl();
            if (providerService.updateProvider(id,proCode,proName,proDesc,proContact,proPhone,proAddress,proFax)){
                this.query(req,resp);
            }
        }
    }


}
